/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package plan.mobile;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import mobile.Call;
import mobile.Message;
import mobile.MobileConsumption;
import plan.Plan;
import turnover.Consumption;
import util.Time;

/**
 *
 * @author Alberto
 */
public class Orange_tp_mini extends Plan {

    private double startQuote = 15d;
    private double minuteCost = 0.18d;
    private double messageCost = 0.15d;

    /*La tarifa es compatible con todos los módulos de ahorro,
     salvo Módulo Fijos 24hrs., e incompatible con todas las
    promociones Orange (excepto con Domingos Orange y la promoción Amigos a 0).*/
    public Orange_tp_mini() {
        super.setName("Orange Tarifa Plana Mini");
        getSavesNoCompatibles().add("save.Orange_Bono_ahorroSMS");
    }

    public double getStartQuote() {
        return startQuote;
    }

    @Override
    public double getCost(Consumption consumption) {
        MobileConsumption mc = (MobileConsumption) consumption;
        List<Long> numList = new LinkedList();
        double total = 0;
        long durationMax = 300 * 60;
        long duration = 0;
        Iterator it = mc.getCallList().listIterator();
        while (it.hasNext()) {
            Call c = (Call) it.next();
            if (c.isSpecial()) {
                total += c.getCost();
            } else if (!this.more75Numbers(numList, c) && !this.overDurationMax(durationMax, duration) && !this.overOneHour(c) && !this.isOutTime(c)) {
                duration += c.getDuration().changeSeconds();
                if (!numList.contains(c.getMobile())) {
                    numList.add(c.getMobile());
                }
            } else if (!this.more75Numbers(numList, c) && !this.overDurationMax(durationMax, duration) && this.overOneHour(c) && !this.isOutTime(c)) {
                duration += 3600;
                total += c.getDuration().changeSeconds() - 3600 * this.minuteCost / 60;
                if (!numList.contains(c.getMobile())) {
                    numList.add(c.getMobile());
                }
            } else {
                total += c.getDuration().changeSeconds() * this.minuteCost / 60;
                total += 0.15d;
            }
        }
        it = mc.getMessList().listIterator();
        while (it.hasNext()) {
            Message m = (Message) it.next();
            if (m.isSpecial()) {
                total += m.getCost();
            } else {
                total += this.messageCost;
            }
        }
        if (total < 15) {
            total = 15d;
        }
        total += total * 0.16d;
        return total;
    }

    private boolean more75Numbers(List<Long> numList, Call c) {
        return (!numList.contains(c.getMobile()) && numList.size() == 75);
    }

    private boolean isOutTime(Call c) {
        return (c.getStart().compareTo(new Time(18, 0, 0)) < 0 && c.getStart().compareTo(new Time(8, 0, 0)) > 0);
    }

    private boolean overOneHour(Call c) {
        return c.getDuration().changeSeconds() > 3600;
    }

    private boolean overDurationMax(long durationMax, long duration) {
        return durationMax <= duration;
    }
}
